#!/bin/bash
shPath=$(cd $(dirname $0);pwd)
cd $shPath

pid=$1

sys_read=`iostat -d -k|tail -n +4|head -n -1|awk -F"[ ]+" '{print $3}'|awk '{sum += $1};END {print sum}'`
sys_write=`iostat -d -k|tail -n +4|head -n -1|awk -F"[ ]+" '{print $4}'|awk '{sum += $1};END {print sum}'`

num=`ps aux|grep $pid |grep -v grep |wc -l`
if [[ $num == "3"  ]];then 
  #pid_read=`pidstat -d -p ${pid}|tail -n1 |awk -F"[ ]+" '{print $4}'`
  #pid_write=`pidstat -d -p ${pid}|tail -n1 |awk -F"[ ]+" '{print $5}'` 
  io_string=`pidstat -d -p ${pid} | tail -n2 | awk 'NR==1'` 
  #echo $io_string 
  array=(${io_string// / })
  read_pos=0
  write_pos=0
  i=0
  for var in ${array[@]}
  do
	let i++
	if [ $var == "kB_rd/s" ];then
	  read_pos=$i
	fi
	if [ $var == "kB_wr/s" ];then
	  write_pos=$i
	fi
  done
  #echo "pos $read_pos $write_pos"
  io_string=`pidstat -d -p ${pid} | tail -n1`
  #echo $io_string 
  array=(${io_string// / })
  i=0
  for var in ${array[@]}
  do
	let i++
	if [ $read_pos == $i ];then
	  pid_read=$var
	fi
	if [ $write_pos == $i ];then
	  pid_write=$var
	fi
  done  
else
  pid_read=0.00
  pid_write=0.00
fi

echo "$sys_read $sys_write $pid_read $pid_write"
